import Vue from "vue";
import VueRouter from "vue-router";
Vue.use(VueRouter);
import { getToken } from "@/utils/tokenData";
import Login from "@/views/Login.vue";
import Home from "@/views/Home.vue";
import Index from "@/views/index.vue";
import List from "@/views/Post/List.vue";
import Publish from "@/views/Post/Publish.vue";
const routes = [
  { path: "/", redirect: "/home" },
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/home/index",
    children: [
      {
        path: "index",
        component: Index,
      },
      {
        path: "post_list",
        component: List,
      },
      {
        //传递了id就是文章编辑，没传就是文章发布
        path: "post_publish/:id?",
        component: Publish,
      },
    ],
  },
];

const router = new VueRouter({
  mode: 'history',
  routes,
});

router.beforeEach((to, from, next) => {
  // console.log(from, to);
  if (to.path === "/login") {
    next();
  } else {
    if (getToken()) {
      next();
    } else {
      next("/login");
    }
  }
});

export default router;
